﻿Imports CrystalReportViewer
Imports org.model.lib.db
Imports org.codegen.win.controls.Grid
Imports ClaimsModel.Services

Namespace Forms.List
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class frmClaimList
        Inherits frmBaseGrid

#Region "Designer"

        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Friend WithEvents ucClaimList As ucClaimList
        Friend WithEvents ssMain As System.Windows.Forms.StatusStrip
        Friend WithEvents tsLblStatus As System.Windows.Forms.ToolStripStatusLabel
        Friend WithEvents tsslKeys As System.Windows.Forms.ToolStripStatusLabel

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.ssMain = New System.Windows.Forms.StatusStrip()
            Me.tsLblStatus = New System.Windows.Forms.ToolStripStatusLabel()
            Me.tsslKeys = New System.Windows.Forms.ToolStripStatusLabel()
            Me.ucClaimList = New ClaimsUi.ucClaimList()
            Me.pnlGrid.SuspendLayout()
            Me.ssMain.SuspendLayout()
            Me.SuspendLayout()
            '
            'pnlGrid
            '
            Me.pnlGrid.Controls.Add(Me.ucClaimList)
            Me.pnlGrid.Size = New System.Drawing.Size(887, 395)
            '
            'ssMain
            '
            Me.ssMain.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsLblStatus, Me.tsslKeys})
            Me.ssMain.Location = New System.Drawing.Point(0, 420)
            Me.ssMain.Name = "ssMain"
            Me.ssMain.Size = New System.Drawing.Size(887, 22)
            Me.ssMain.TabIndex = 1
            Me.ssMain.Text = "ssMain"
            '
            'tsLblStatus
            '
            Me.tsLblStatus.Font = New System.Drawing.Font("Segoe UI", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
            Me.tsLblStatus.Name = "tsLblStatus"
            Me.tsLblStatus.Size = New System.Drawing.Size(26, 17)
            Me.tsLblStatus.Text = "OK"
            '
            'tsslKeys
            '
            Me.tsslKeys.Name = "tsslKeys"
            Me.tsslKeys.Size = New System.Drawing.Size(48, 17)
            Me.tsslKeys.Text = "tsslKeys"
            '
            'ucClaimList
            '
            Me.ucClaimList.Dock = System.Windows.Forms.DockStyle.Fill
            Me.ucClaimList.Location = New System.Drawing.Point(0, 0)
            Me.ucClaimList.Name = "ucClaimList"
            Me.ucClaimList.Size = New System.Drawing.Size(887, 395)
            Me.ucClaimList.TabIndex = 0
            '
            'frmClaimList
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(887, 442)
            Me.Controls.Add(Me.ssMain)
            Me.Name = "frmClaimList"
            Me.ShowConfigButton = True
            Me.ShowExcelButton = True
            Me.ShowPrintButton = True
            Me.ShowSearch = True
            Me.Text = "Απαιτήσεις"
            Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
            Me.Controls.SetChildIndex(Me.ssMain, 0)
            Me.Controls.SetChildIndex(Me.pnlGrid, 0)
            Me.pnlGrid.ResumeLayout(False)
            Me.ssMain.ResumeLayout(False)
            Me.ssMain.PerformLayout()
            Me.ResumeLayout(False)
            Me.PerformLayout()

        End Sub

#End Region

#Region "Standard Code"

        Private Sub ConnectDatabase()

            Me.Text = "Claims System - " & My.Application.CurrentDatabaseDisplay

            If ClaimsModel.DBVersion.SYSTEM_DATABASE_VERSION > 0 Then

                Dim backupSql = <backupSql>
BACKUP DATABASE <%= My.Application.CurrentDatabase %>DB
TO  DISK = N'<%= My.Application.dbBackupPath %><%= My.Application.CurrentDatabase %>UpgradeFrom{0}To{1}.bak'
WITH NOFORMAT, INIT,  NAME = N'<%= My.Application.CurrentDatabase %>-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
</backupSql>

                DBUpdater.dbUpdateVersion(DBUtils.Current, ClaimsModel.DBVersion.SYSTEM_DATABASE_VERSION, _
                                          GetType(ClaimsModel.DBVersion).Assembly, backupSql.Value)
            End If

            Me.grdData.DataSource = Nothing
            Me.grdData.requery()

            If Not My.Application.initialAllClaimsUpdateDone Then
                Dim UpdateClaimAmountsService As New UpdateClaimAmountsService
                UpdateClaimAmountsService.updateAmounts()
                My.Application.initialAllClaimsUpdateDone = True
            End If

        End Sub

        Private Sub frmClaimList_gridRowCountChanged(ByVal sender As Object) Handles Me.gridRowCountChanged

            Me.tsLblStatus.Text = Me.grdData.RowCount & " Claims"

        End Sub

        Private Sub buildReportMenu()

            'CriterionMetadata.defaultCriterionHandler = IICReportsDataSourceHandler.instance

            Dim ret As List(Of ReportMatadata) = My.Application.reportsCache.getSystemReports

            For Each cr As ReportMatadata In ret
                Me.addToReportMenu(cr.DisplayName, AddressOf PrintReport, cr.ReportKey)
            Next

        End Sub

        Private Sub frmClaimList_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                   Handles Me.Load

            Me.ConnectDatabase()

            Me.AllowAddNew = False
            Me.AllowDelete = False

            Call Me.setEditButtonText("View Claim")
            Call Me.buildReportMenu()
            Me.addToolbarActionButton("Claims Batch Update", AddressOf ClaimsBatchUpdate)

            Me.addToolbarActionButton("Connect Database", AddressOf SelectAndConnectDatabase)
            
            Dim mnClaims As ToolStripDropDownButton = New ToolStripDropDownButton
            mnClaims.Text = "Claims..."

            Dim t As ToolStripItem = mnClaims.DropDownItems.Add("Κατά Α/Α", Nothing, AddressOf ClaimViewBy)
            t.Tag = "byaa"
            CType(t, ToolStripMenuItem).Checked = True

            t = mnClaims.DropDownItems.Add("Κατά Ονομα Ενάγοντα", Nothing, AddressOf ClaimViewBy)
            t.Tag = "name"

            t = mnClaims.DropDownItems.Add("Κατά Ονομα Εναγομένου", Nothing, AddressOf ClaimViewBy)
            t.Tag = "reverseName"

            t = mnClaims.DropDownItems.Add("Κατά Ταξινόμηση", Nothing, AddressOf ClaimViewBy)
            t.Tag = "taksi"

            t = mnClaims.DropDownItems.Add("Κατά Αρ. Απαίτησης", Nothing, AddressOf ClaimViewBy)
            t.Tag = "claimno"

            t = mnClaims.DropDownItems.Add("Κατά Ονομα Πελάτη Εταιρείας", Nothing, AddressOf ClaimViewBy)
            t.Tag = "customername"

            t = mnClaims.DropDownItems.Add("Μερίσματα", Nothing, AddressOf ClaimViewBy)
            t.Tag = "payments"

            t = mnClaims.DropDownItems.Add("Επαληθεύσεις", Nothing, AddressOf ClaimViewBy)
            t.Tag = "epal"

            Me.tlStripList.Items.Insert(0, mnClaims)
            Me.tsslKeys.Text = "↑↓ Κίνηση:, Enter or Ctrl-V: Θεώρηση"
            Me.grdData.gpSearchFields.Add("SN")
            Me.tsTxtSearch.Visible = True
            Me.tsLblSearch.Visible = True

        End Sub

        ''' <summary>
        ''' This function is common to all forms that inherit from class frmBaseGrid
        ''' It priovides a common name to the underlying grid control that shows the records
        ''' </summary>
        Public Overrides Function grdData() As CGBaseGrid
            Return Me.ucClaimList.grdClaim
        End Function

        Protected Sub DeleteRecordConfirmed(ByVal sender As Object) Handles Me.gridDeleteRecordConfirmed


        End Sub

#End Region

        Private Sub ClaimsBatchUpdate(ByVal sender As Object, ByVal e As EventArgs)
            'andreas

            '1. SHOW FORM.
            Using f As New frmClaimsBatchUpdate
                If f.ShowDialog = DialogResult.OK Then
                    Try

                    
                        Dim batchPaymentService As New BatchPaymentService
                        batchPaymentService.doPayment(CType(f.AppliesTo.intValue, 
                                                      ClaimsModel.Services.BatchPaymentService.PaymentAppliesTo), _
                                                      CDec(f.Percentage.decimalValue), _
                                                      f.ExecutionDate.DateValue.Value)

                        winUtils.MsgboxInfo("Batch Process Completed Successfully")

                    Catch ex As Exception
                        Dim newmsg As String = String.Format("Error executing batchPaymentService.doPayment with params {0},{1} ", _
                                                    f.Percentage.decimalValue, _
                                                         f.AppliesTo.intValue) & vbCrLf & ex.Message

                        Throw New ApplicationException(newmsg, ex)
                    End Try
                Else
                    '2. IF CANCELLED, EXIT
                    Exit Sub

                End If

            End Using


        End Sub

        Private Sub SelectAndConnectDatabase(ByVal sender As Object, ByVal e As EventArgs)
            Dim d As New DBSelector

            If d.selectDatabaseFromUI Then
                Call ConnectDatabase()
            End If

        End Sub

        Private Sub PrintReport(ByVal sender As Object, ByVal e As EventArgs)
            Dim tsmi As ToolStripItem = CType(sender, ToolStripItem)
            Dim rptKey As String = CStr(tsmi.Tag)

            Dim cv As New CrystalReportViewer.CRReportViewer
            Dim rpt As ReportMatadata = My.Application.reportsCache.getSystemReportByKey(rptKey)
            'cv.IReportInstanceLoader = IICReportsDataSourceHandler.instance
            cv.ReportConnString = DBUtils.Current.ConnString
            cv.ReportMetaData = rpt
            cv.askForCriteriaAndShowReport()

        End Sub

        Private Sub setupView(ByVal tsItem As ToolStripMenuItem, ByVal lTsItemTag As String)

            Dim mnClaims As ToolStripDropDownButton = CType(tsItem.OwnerItem, ToolStripDropDownButton)
            For Each t As ToolStripMenuItem In mnClaims.DropDownItems
                t.Checked = False
            Next
            tsItem.Checked = True

            If (lTsItemTag = "name") Then
                Me.cleaWhereClause()
                Me.grdData.Columns("ClName").DisplayIndex = 0

            ElseIf (lTsItemTag = "reverseName") Then
                Me.cleaWhereClause()
                Me.grdData.Columns("DefName").DisplayIndex = 0

            ElseIf (lTsItemTag = "taksi") Then
                Me.cleaWhereClause()
                Me.grdData.Columns("ClassCd").DisplayIndex = 0

            ElseIf (lTsItemTag = "claimno") Then
                Me.cleaWhereClause()
                Me.grdData.Columns("ClaimNo").DisplayIndex = 0

            ElseIf (lTsItemTag = "customername") Then
                Me.cleaWhereClause()
                Me.grdData.Columns("CusName").DisplayIndex = 0

            ElseIf (lTsItemTag = "byaa") Then
                Me.cleaWhereClause()
                Me.grdData.Columns("SN").DisplayIndex = 0

            ElseIf (lTsItemTag = "payments") Then
                Using frmp As New frmBatchPaymentList
                    frmp.GridMode = enumGridFormMode.MODE_SELECT
                    If frmp.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        If frmp.grdData.SelectedRows.Count > 0 Then
                            Dim PaymentId As Integer = CInt(frmp.grdData.SelectedRows(0).Cells("PaymentId").Value)
                            Dim snsql = <sql>
s_n in (select s.s_n from PaymentClaim s where s.PaymentId=<%= PaymentId %>)
                                        </sql>

                            CType(Me.grdData, CGSQLGrid).gpWhereclause = snsql.Value
                            Me.grdData.loadGrid()

                        End If
                    End If

                End Using


            ElseIf (lTsItemTag = "epal") Then
                CType(Me.grdData, CGSQLGrid).gpWhereclause = "epal_no>0"
                Me.grdData.loadGrid()

            End If

        End Sub

        Private Sub cleaWhereClause()
            If CType(Me.grdData, CGSQLGrid).gpWhereclause <> "" Then
                CType(Me.grdData, CGSQLGrid).gpWhereclause = ""
                Me.grdData.loadGrid()
            End If
        End Sub

        Private Sub ClaimViewBy(ByVal sender As Object, ByVal e As EventArgs)

            Dim tsItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
            Dim lTsItemTag As String = CStr(tsItem.Tag)

            setupView(tsItem, lTsItemTag)

        End Sub


        Private Sub frmClaimList_KeyDown(ByVal sender As Object, _
                                         ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

            If e.Control And e.KeyCode = Keys.V Then
                Call ListEditRecord(Me.grdData.IdValue)
            End If

        End Sub


    End Class

End Namespace

